from django.test import TestCase
from django.core.urlresolvers import reverse

from .models import Test, Flag

class TestModelTests(object):
    """
    >>> test = Test()
    >>> test.flags.all()
    []
    >>> test.flags.add(Flag.objects.get_or_create(pk="foo")[0])
    >>> test.flags.all()
    [<Flag: foo>]
    >>> test._flags
    set([])
    >>> test.load_m2m()
    >>> test._flags
    set([u'foo'])
    """
    #TODO: add more tests

# TODO: things to test
# deleted tests should not show in test list.

class ViewTests(TestCase):
    fixtures = ["test_tests", "test_useragents", "test_results", "test_auth"]

    def testTestList(self):
        response = self.client.get(reverse('test-list', kwargs=dict(page="1")))
        self.assertContains(response, "List of tests")

    def testTestDetail(self):
        response = self.client.get(reverse('test-detail', kwargs=dict(object_id="unit/layout_pass.html")))
        self.assertContains(response, "Test unit/layout_pass.html Detail")

    def testTestSearch(self):
        path = reverse('test-search')
        response = self.client.get(path)
        self.assertContains(response, "Search for tests")
        def assertNumResults(response, count):
            self.assertContains(response, "%s result" % count)
            self.assertContains(response, "found.")
        assertNumResults(response, 0)

        response = self.client.get(path, {"file": ""})
        assertNumResults(response, 34)

        response = self.client.get(path, {"file": "", "type": "mochitest"})
        assertNumResults(response, 4)

        response = self.client.get(path, {"type": "mochitest"})
        assertNumResults(response, 4)

        response = self.client.get(path, {"file": "nonexistent"})
        assertNumResults(response, 0)

        response = self.client.get(path, {"file": "layouttests"})
        assertNumResults(response, 20)

        response = self.client.get(path, {"flag": "ltwait"})
        assertNumResults(response, 4)

        # TODO: test specs search, and adding filter together (spec+flag)

        response = self.client.get(path, {"file": "unit/layouttests/innerText/tables-1.html"})
        assertNumResults(response, 1)

        # Deleting a test is denied without permissions
        response = self.client.get(path, {"file": "unit/layouttests/innerText/tables-1.html", "delete": ""})
        self.assertContains(response, "Deletion denied", status_code=403)

        self.assertFalse(self.client.login(username="admin", password="invalidpassword"))

        self.assertTrue(self.client.login(username="admin", password="admin"))

        test_file = "unit/layouttests/innerText/tables-1.html"
        test = Test.objects.get(file__contains=test_file)
        response = self.client.get(path, {"file": test_file, "delete": ""})
        self.assertEqual(Test.objects.filter(file__contains=test_file).count(), 0)
        # Redirects to same page
        # XXX should be 0 results
        assertNumResults(response, 1)

        self.client.logout()
        response = self.client.get(path, {"delete": ""})
        self.assertContains(response, "Deletion denied", status_code=403)

    # TODO: add tests for pagination.
